package com.api.gbhg.dq.dao;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.general.TimeUtil;
import weaver.general.Util;

import java.util.UUID;

public class BudgetAccountDao {

    public static final Logger LOGGER = LoggerFactory.getLogger(BudgetAccountDao.class);

    //查询部门
    public RecordSet getDepartmentById(String id) {
        RecordSet rs = new RecordSet();
        String StrSQL = "select * from hrmresource a left JOIN hrmdepartment b on a.departmentid = b.id where a.id =" + id;
        rs.executeQuery(StrSQL);
        return rs;
    }

    //查询预算科目下发所有信息
    public RecordSetTrans getYskxmxf(Integer pageNum, Integer pageSize) {
        RecordSetTrans rts = new RecordSetTrans();
        StringBuffer sb = new StringBuffer();
        String sql = "select * from uf_yskxmxf";
        sb.append(sql);
        try {
            //通过分页公式计算下一页在哪一行开始
            //如1，10，1-1 = 0，那第一条开始显示下面十条，第一页
            //如2，10，(2-1)乘以10 = 10，那就是第十条开始显示下面十条，第二页
            // 判断是mysql 还是sqlserver
            pageNum = (pageNum - 1) * pageSize;
            if ("sqlserver".equals(rts.getDBType())) {
                sb.append(" order by id offset " + pageNum + " rows fetch next " + pageSize + " rows only");
            } else if ("mysql".equals(rts.getDBType())) {
                sb.append(" LIMIT " + pageNum + "," + pageSize + "");
            }
            LOGGER.info("查询预算科目下发所有信息sql:--->" + sb.toString());
            rts.executeQuery(sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rts;
    }

    //查询预算科目下发所有信息条数
    public int getYskxmxfCount() {
        RecordSet rs = new RecordSet();
        String StrSQL = "select COUNT(1)AS num from uf_yskxmxf";
        rs.executeQuery(StrSQL);
        rs.next();
        return rs.getInt("num");
    }

    //查询预算科目下发所有信息
    public RecordSetTrans getYskxmxfById(String id, Integer pageNum, Integer pageSize) {
        RecordSetTrans rts = new RecordSetTrans();
        StringBuffer sb = new StringBuffer();
        String sql = "select * from uf_yskxmxf where ry like '%" + id + "%'";
        sb.append(sql);
        try {
            //通过分页公式计算下一页在哪一行开始
            //如1，10，1-1 = 0，那第一条开始显示下面十条，第一页
            //如2，10，(2-1)乘以10 = 10，那就是第十条开始显示下面十条，第二页
            // 判断是mysql 还是sqlserver
            pageNum = (pageNum - 1) * pageSize;
            if ("sqlserver".equals(rts.getDBType())) {
                sb.append(" order by id offset " + pageNum + " rows fetch next " + pageSize + " rows only");
            } else if ("mysql".equals(rts.getDBType())) {
                sb.append(" LIMIT " + pageNum + "," + pageSize + "");
            }
            LOGGER.info("查询预算科目下发所有信息sql:--->" + sb.toString());
            rts.executeQuery(sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rts;
    }

    //查询预算科目下发所有信息条数
    public int getYskxmxfCountById(String id) {
        RecordSet rs = new RecordSet();
        String StrSQL = "select COUNT(1)AS num from uf_yskxmxf where ry like '%" + id + "%'";
        rs.executeQuery(StrSQL);
        rs.next();
        return rs.getInt("num");
    }

    public void insertYskxmxfs(String dw, String ry, String xmmc, String ysje) {
        //插入增值税专票台账
        String currentDateString = TimeUtil.getCurrentDateString();
        String currentTimeString = TimeUtil.getOnlyCurrentTimeString();
        //查询建模Id
        String sql = "select id from modeinfo where formid=(select id from workflow_bill where tablename = 'uf_yskxmxf')";
        RecordSet rss = new RecordSet();
        rss.executeQuery(sql);
        rss.next();
        int modeid = Util.getIntValue(rss.getString(1));
        String uuid = UUID.randomUUID().toString();
        RecordSet rs = new RecordSet();
        rs.executeUpdate("insert into uf_yskxmxf(dw,ry,xmmc,ysje,modedatacreater,modedatacreatedate,modedatacreatertype,modedatacreatetime,formmodeid,modeuuid) values(?,?,?,?,?,?,?,?,?,?)", dw, ry, xmmc, ysje, "1", currentDateString, "0", currentTimeString, modeid, uuid);
    }

    public void updateYskxmxfs(String id, String lcid) {
        RecordSet rs = new RecordSet();
        rs.executeUpdate("update uf_yskxmxf set lcid = " + "'" + lcid + "'" + " where id = " + "'" + id + "'");
    }

    //查询部门名称
    public String getDepartmentNameById(String id) {
        String departmentname = "";
        RecordSet rs = new RecordSet();
        String StrSQL = "select * from hrmdepartment where id = " + id;
        rs.executeQuery(StrSQL);
        if (rs.next()) {
            departmentname = rs.getString("departmentname");
        }
        return departmentname;
    }

    //获取流程状态
    public String getStateByrequestId(String requestid) {
        String nodetype = "4";
        RecordSet rs = new RecordSet();
        String sql = "select * from workflow_currentoperator where requestid = " + "'" + requestid + "'" + " order by operatetime desc";
        rs.executeQuery(sql);
        if (rs.next()) {
            String workflowid = rs.getString("workflowid");
            String nodeid = rs.getString("nodeid");
            RecordSet rss = new RecordSet();
            String strSql = "select * from workflow_flownode where workflowid = " + "'" + workflowid + "'" + " and nodeid = " + "'" + nodeid + "'";
            rss.executeQuery(strSql);
            if (rss.next()) {
                nodetype = rss.getString("nodetype");
            }
        }
        return nodetype;
    }

    //查询人员名称
    public String getHrmresourceById(String id) {
        String name = "";
        RecordSet rs = new RecordSet();
        String StrSQL = "select * from hrmresource where id = " + id;
        rs.executeQuery(StrSQL);
        if (rs.next()) {
            name = rs.getString("lastname");
        }
        return name;
    }
}
